﻿using System;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.SpecializedProperties;
using PageTypeBuilder;
using TemplateFoundation.Tabs;

namespace TemplateFoundation.PageTypes
{
    /// <summary>
    /// Used as a data container or presentation page for a contact, such as a contact person
    /// </summary>
    public abstract class PersonPageBase : ContactPageBase, IPersonPage
    {
        /// <summary>
        /// Gets or sets the first name
        /// </summary>
        [PageTypeProperty(
            EditCaption = "First name",
            HelpText = "The person's first name",
            UniqueValuePerLanguage = false,
            SortOrder = 100,
            Type = typeof (PropertyString),
            Tab = typeof (ContactDetailsTab))]
        public virtual string FirstName { get; set; }

        /// <summary>
        /// Gets or sets the last name
        /// </summary>
        [PageTypeProperty(
            EditCaption = "Last name",
            HelpText = "The person's last name",
            UniqueValuePerLanguage = false,
            SortOrder = 200,
            Type = typeof (PropertyString),
            Tab = typeof (ContactDetailsTab))]
        public virtual string LastName { get; set; }

        /// <summary>
        /// Gets the person's full name by concatenating first and second name
        /// </summary>
        /// <remarks>Page name is used if first and last name hasn't been specified</remarks>
        [PageTypeProperty(DisplayInEditMode = false)]
        public override string Name
        {
            get
            {
                var fullName =  string.Format("{0} {1}", FirstName, LastName).Trim();

                if (!string.IsNullOrEmpty(fullName))
                {
                    return fullName;
                }

                return PageName;
            }
            set
            {
                throw new NotSupportedException("Cannot set Name property for person pages, use the FirstName and LastName properties instead");
            }
        }

        /// <summary>
        /// Gets or sets the job title
        /// </summary>
        [PageTypeProperty(
            EditCaption = "Job title",
            HelpText = "The person's job title",
            UniqueValuePerLanguage = true,
            SortOrder = 300,
            Type = typeof (PropertyString),
            Tab = typeof (ContactDetailsTab))]
        public virtual string JobTitle { get; set; }

        /// <summary>
        /// Gets or sets the image to use as the portrait of the person
        /// </summary>
        /// <remarks>Overriden to provide different caption and help text</remarks>
        [PageTypeProperty(
            EditCaption = "Portrait image",
            HelpText = "Portrait image for this person",
            UniqueValuePerLanguage = false,
            SortOrder = 700,
            Type = typeof (PropertyImageUrl),
            Tab = typeof (ContactDetailsTab))]
        public override string ContactImage { get; set; }
    }
}